<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

$username = $_SESSION['username'];
if (!$username) {
    return_json(0,'暂未登录，无权限');
}

$mode = intval($_POST['mode']);

if ($mode != 1 && $mode != 2) {
    return return_json(0,'参数错误');
}

$database->update("sys_config",['value'=>$mode],['name'=>'safe_mode']);
if($mode == 1){
    require_once 'getip.php';
    $client_ip = getClientIP();
    $timestamp = time();
    $database->insert("secure_info",["user"=>$username,"ip"=>$client_ip,"times"=>$timestamp,"content"=>"修改防火墙模式为黑名单。"]);
}else if($mode == 2){
    require_once 'getip.php';
    $client_ip = getClientIP();
    $timestamp = time();
    $database->insert("secure_info",["user"=>$username,"ip"=>$client_ip,"times"=>$timestamp,"content"=>"修改防火墙模式为白名单。"]);
}

return return_json(1,'Success');

function return_json($code = 1,$msg = '',$data = [])
{
    echo json_encode([
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ]);
}